//
//  WFEmoticonTool.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/20.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

let numbersOfPages = 20


class WFEmoticonTool: NSObject {

    //提供单例
    static let sharedTools = WFEmoticonTool()
    
    //私有化构造
    private override init() {
        super.init()
    }
    
    //获取bundle对象
    private lazy var emoticonBundle:NSBundle = {
        //获取路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
        
        let bundle = NSBundle(path: path)
        
        return bundle!
    }()
    
    lazy var allEmoticonsArray:[[[WFEmoticon]]] = {
        return [
            self.pagesWithEmoticons(self.defaultEmoticonArray),
            self.pagesWithEmoticons(self.emojiEmoticonArray),
            self.pagesWithEmoticons(self.lxhEmoticonArray)
        
        
        ]
    }()
    
    
    //读取默认表情
    private lazy var defaultEmoticonArray:[WFEmoticon] = {
        return self.loadEmoticonsWithPath("default/info.plist")
    }()
    
    //读取emoji表情
    private lazy var emojiEmoticonArray:[WFEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    //读取lxh表情
    private lazy var lxhEmoticonArray:[WFEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    // MARK:  - 读取表情通用方法
    private func loadEmoticonsWithPath(subPath:String)->[WFEmoticon]{
        
        //获取bundle对象
        let bundle: NSBundle = self.emoticonBundle
        
        let path = bundle.pathForResource(subPath, ofType: nil)!
        let emoticonDicArray = NSArray(contentsOfFile: path)!
        
        var tempArray = [WFEmoticon]()
        
        for emoticonDic in emoticonDicArray {
            let emoticon = WFEmoticon(dic: emoticonDic as! [String : AnyObject])
            //如果是图片路径,需要进行处理
            if emoticon.type == "0" {
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = lastPath + "/" + emoticon.png!
            }
            tempArray.append(emoticon)
        }
        return tempArray
        
    }
    
    // MARK:  - 把表情数据拆分成一个二维数组
    private func pagesWithEmoticons(emoticons:[WFEmoticon])->[[WFEmoticon]]{
        //根据图片的个数计算页数
        let pages = (emoticons.count - 1) / numbersOfPages + 1
        var tempArr = [[WFEmoticon]]()
        for i in 0..<pages {
            //截取的索引
            let loc = i * numbersOfPages
            
            var len = numbersOfPages
            
            //假设长度不够
            if loc + len > emoticons.count {
                len = emoticons.count - loc
            }
            
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as![WFEmoticon]
            tempArr.append(subArray)
        }
        return tempArr
    }
    
    // MARK:  - 根本表情描述字符串查找表情模型
    func selectEmoticonWithChs(chs:String)->WFEmoticon?{
        
         let defaultEmoticon = defaultEmoticonArray.filter { (emoticon) -> Bool in
            //根据指定的条件进行过滤
            return emoticon.chs! == chs
        }.first
        
        let lxhEmoticon = lxhEmoticonArray.filter { (emoticon) -> Bool in
            //根据指定的条件进行过滤
            return emoticon.chs! == chs
        }.first
        
        if defaultEmoticon != nil {
            
            return defaultEmoticon!
        }
        
        if lxhEmoticon != nil {
            return lxhEmoticon!
        }
        
        return nil
    }

}
